home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14952 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  102 lines

  1. Path: news.halcyon.com!usenet
  2. From: Glen Parker <glenebob@halcyon.com>
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: New printf - like function, is it possible?
  5. Date: Tue, 02 Apr 1996 12:43:56 -0800
  6. Organization: Computer Systems Contracting
  7. Message-ID: <3161918C.72D4@halcyon.com>
  8. References: <4eqb3a$2r5@pan.otol.fi> <311240d2.593861@news.demon.co.uk>
  9. NNTP-Posting-Host: blv-pm2-ip18.halcyon.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (WinNT; I)
  14.  
  15. > >Do you C-gurus have any solutions to this problem?
  16. > >
  17. > >I am programming under MS Windows 3.1 (this is NOT an OS spesific
  18. > >question!)and I would like to create
  19. > >some kind of modified print function, which syntax should be something
  20. > >like normal printf; because in Windows printf can't be used, everything
  21. > >printed to screen has to come through message boxes etc, and before
  22. > >displaying anything, the string to displayed has to be formed. I have
  23. > >created my own printing function, which wants parameters string and winID
  24. > >to know where to put that string, something like this:
  25. > >
  26. > >sprintf(printStr, "some sfuff, variables %d, %f, etc", var1, var2);
  27. > >PrintToWin(DEBUG_WIN_1, printStr);
  28. > >
  29. > >However, I want to write a "better" printing function, where string to be
  30. > >formed doesn't need to be formatted first, it can be displayed and formed
  31. > >at the same time in the same syntax like printf, something like these:
  32. > >
  33. > >PrintToWin(DEBUG_WIN_1, "some stuff etc, %d, %f, %s, var1, var2, str1);
  34. > >PrintToWin(OTHER_WIN, "anything that printf accepts");
  35. > >
  36. > >So I want to print almost anything with this function without having
  37. > >to write many functions to different argument types/number of arguments.
  38. > >This should be possible in C++ (is it?), but is it possible in C ?
  39. > >Can this be achieved by using void pointers and macros etc., or should
  40. > >I give up and don't waste my time on this?
  41. > >
  42. > >Any help would be greatly appreciated. Thanks.
  43.  
  44.  
  45. Try this code:
  46. ---------------------------------------------
  47. char*         Text;
  48.  
  49. Text = (char*)malloc(6);    //get memory
  50. sprintf(Text, "Hello");     //and stuff it
  51.                             //with formatted text
  52.  
  53. MessageBox(GetFocus(), Text, "Debug", 0);
  54. free((void*)Text);          //and free it again
  55. ---------------------------------------------
  56. or
  57. ---------------------------------------------
  58. char*         Text;
  59.  
  60. Text = (char*)malloc(6);    //get memory
  61. sprintf(Text, "Hello");     //and stuff it
  62.                             //with formatted text
  63.  
  64. TextOut(SomeDC, 0, 0, Text, strlen(Text));
  65. free((void*)Text);          //and free it again
  66. ---------------------------------------------
  67.  
  68.  
  69. here is the documentation I got from my Borland C++ help file:
  70.  
  71. ---------------------------------------------
  72. sprintf
  73.  
  74. Syntax
  75.  
  76. #include <stdio.h>
  77. int sprintf(char *buffer, const char *format[, argument, ...]);
  78.  
  79. Description
  80.  
  81. Writes formatted output to a string.
  82.  
  83. Note:    For details on format specifiers, see printf.
  84.  
  85. sprintf accepts a series of arguments, applies to each a format specifier contained in the 
  86. format string pointed to by format, and outputs the formatted data to a string.
  87. sprintf applies the first format specifier to the first argument, the second to the second, 
  88. and so on. There must be the same number of format specifiers as arguments.
  89.  
  90. Return Value
  91.  
  92. On success, sprintf returns the number of bytes output. The return value does not include 
  93. the terminating null byte in the count. 
  94. On error, sprintf returns EOF.
  95. --------------------------------------------------
  96.  
  97. Good Luck
  98.  
  99. Glen Parker
  100.  
  101.  
  102.